<template>
  <AyList :list-options="listOptions" ref="listComponent" />
</template>

<script>
  import { async } from '../../biz/demo/schemas';
  import AyList from '/@/components/AyUI/AyList.vue';
  export default {
    name: '${entity?lower_case}List',
    components: {
      AyList,
    },
    data() {
      return {
        // 列表配置对象，支持所有vxe-grid的配置选项
        listOptions: {
         gridOptions:{
          id: 'DemoTwoList',
          formConfig: {
            titleWidth: 100,
            titleAlign: 'left',
            items: [
              <#list table.fields as field>
              { field: '${field.propertyName}', title: '${field.comment}', span: 6, itemRender: { name: 'VxeInput' } },
              </#list>

            ],
          },
          columns: [
            { type: 'checkbox', width: 60 },
            { type: 'seq', width: 70 },
              <#list table.fields as field>
              { field: '${field.propertyName}', title: '${field.comment}', minWidth: 200 },
              </#list>
          ],

          toolbarButtons: [
            { name: '新增', status: 'primary', icon: 'vxe-icon-add', code: 'add', onClick: 'handleAdd', auth: ['admin', '${entity?lower_case}:add'] },
            { name: '批量删除', status: 'danger', icon: 'vxe-icon-delete', code: 'delete', onClick: 'handleDelete', auth: ['admin', '${entity?lower_case}:delete'] },
          ],

          actionButtons: [
            { name: '编辑', code: 'edit', status: 'primary', icon: 'vxe-icon-edit', mode: 'text', onClick: 'handleRowEdit', auth: ['admin', '${entity?lower_case}:edit'] },
            { name: '删除', code: 'delete', status: 'error', icon: 'vxe-icon-delete', mode: 'text', onClick: 'handleRowDelete', auth: ['admin', '${entity?lower_case}:delete'] },
            { name: '查看详情', code: 'view', status: 'primary', icon: 'vxe-icon-eye', mode: 'text', onClick: 'handleView', auth: ['admin', '${entity?lower_case}:view'] },
          ],

          // 组件属性：API地址
          apiUrl: '${entity?lower_case}.get${entity}List',
         }
        },
      };
    },
    methods: {
      // 工具栏按钮方法
      handleAdd() {
        this.$router.push({ name: '${entity?lower_case}:add' });
      },
      async handleDelete() {
        const selectedRows = this.$refs.listComponent.getSelectedRows();
        console.log('selectedRows', selectedRows);
        if (!selectedRows || selectedRows.length === 0) {
          this.$message?.warning('请选择要删除的数据');
          return;
        }

        const ids = [];
        selectedRows.map((item) => {
          ids.push(item.id);
        });
        this.delete(ids);
      },
      delete(ids){
          this.$VxeUI.modal.confirm({
          title: '删除提示框',
          content: '是否确认删除,删除后无法恢复?',
          mask: false,
          lockView: false,
          onConfirm: async () => {
            const res = await this.$api.${entity?lower_case}.delete${entity}({ ids: ids });
            if (res.code == 0) {
              this.$VxeUI.modal.message({ content: '删除成功', status: 'success' });
              this.loadData();
            } else {
              this.$VxeUI.modal.message({ content: res.msg, status: 'error' });
            }
          },
        });
      },
      // 操作按钮方法
      handleRowEdit(row) {
         this.$router.push({ name: '${entity?lower_case}:add' ,query:{
          code:row.code
        }
      });
      },
      async handleRowDelete(row) {
       this.delete( [row.id] );
      },

      // 其他操作方法
      handleView(row) {
        this.$router.push({ name: '${entity?lower_case}:view' ,query:{
          code:row.code
        }
      });
      },
      // 重新列表加载数据
      async loadData() {
        this.$refs.listComponent.loadData();
      },
    },
  };
</script>
